package com.lagou;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class Hbaseclient {

    Configuration conf;

    Connection conn;

    HBaseAdmin admin;

    public static void main(String[] args) {
        Hbaseclient hbaseclient = new Hbaseclient();

        try {
            hbaseclient.initClient();
            hbaseclient.createTable();
            hbaseclient.initData();
            // hbaseclient.deleteData("uid1", "uid2");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            hbaseclient.destroyClient();
        }

    }

    void initClient() throws IOException {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "linux121,linux122");
        conf.set("hbase.zookeeper.property.clientPort","2181");

        conn = ConnectionFactory.createConnection(conf);
    }

    void destroyClient() {
        if (admin != null) {
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 创建表
     * @throws IOException
     */
    void createTable() throws IOException {
        admin = (HBaseAdmin) conn.getAdmin();

        // 创建表描述器器
        HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("relationship"));

        // 设置列列族描述器器
        tableDescriptor.addFamily(new HColumnDescriptor("friends"));

        // 执⾏行行创建操作
        admin.createTable(tableDescriptor);
    }

    /**
     * 初始化用户数据
     */
    void initData() throws IOException {
        Table relationship = conn.getTable(TableName.valueOf("relationship"));

        Put uid1 = new Put(Bytes.toBytes("uid1"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid2"), Bytes.toBytes("uid2"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid5"), Bytes.toBytes("uid5"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid7"), Bytes.toBytes("uid7"));

        Put uid2 = new Put(Bytes.toBytes("uid2"));
        uid2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid1"), Bytes.toBytes("uid1"));
        uid2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid3"), Bytes.toBytes("uid3"));
        uid2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid6"), Bytes.toBytes("uid6"));
        uid2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid10"), Bytes.toBytes("uid10"));

        relationship.put(new ArrayList<>(Arrays.asList(uid1, uid2)));
    }

    void deleteData(String uid, String friend) throws IOException {
        Table relationship = conn.getTable(TableName.valueOf("relationship"));
        Delete delete = new Delete(Bytes.toBytes(uid));
        delete.addColumn(Bytes.toBytes("friends"), Bytes.toBytes(friend));

        relationship.delete(delete);
    }
}
